package scau.osdesign.controller;

import javafx.application.Platform;
import javafx.concurrent.Worker;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TextArea;
import javafx.scene.image.ImageView;
import org.kordamp.bootstrapfx.BootstrapFX;
import scau.osdesign.LaunchMain;
import scau.osdesign.model.Philosopher;
import scau.osdesign.view.ViewArranger;

import java.util.Vector;

public class Controller {

    public static boolean isStop = true;

    @FXML
    public void play(){
        isStop = false;
        if(LaunchMain.service.getState() != Worker.State.READY){
            callMutiCallPlayAlert();
            return ;
        }
        LaunchMain.service.start();
    }

    @FXML
    public void pause(){
        for(int i = 0; i < 5;i++){
            LaunchMain.philosophers[i].isPaused = true;
        }
        LaunchMain.simer.isPaused = true;
        callPausePlayAlert();
    }

    @FXML
    public void runContinue(){
        callContinuePlayAlert();
        for(int i = 0 ;i<5;i++){
            LaunchMain.philosophers[i].awakeSelf();
        }
        LaunchMain.simer.awakeSelf();
    }

    @FXML
    public void stop(){
        if(LaunchMain.philosophers!= null && LaunchMain.philosophers[0]!=null){
            isStop = true;
            for(int i = 0 ;i<5;i++){
                LaunchMain.philosophers[i].stop();
            }
            LaunchMain.simer.stop();
            Vector<String> vector = new Vector<>();
            vector.addElement("终止了当前动画");
            Platform.runLater(()->{
                ViewArranger.updateState(LaunchMain.philosophers,LaunchMain.forks,vector);
            });
            Platform.runLater(()->{});
            LaunchMain.reStart();
            callStopPlayAlert();
        }else{
            return ;
        }
    }

    @FXML
    public void backToMainMeau(){
        if( !isStop &&LaunchMain.philosophers!= null && LaunchMain.philosophers[0]!=null){
            stop();
        }
        LaunchMain.stageSim.close();
        LaunchMain.mainStage.show();
        TextArea textArea = (TextArea) LaunchMain.simServices.lookup("#log_area");
        textArea.clear();

    }

    public void callStopPlayAlert(){
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setContentText("您终止了当前动画,想要再次开始请点击开始按钮");
        alert.setHeaderText("动画演示终止");
        alert.showAndWait();
    }

    public void callPausePlayAlert(){
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setHeaderText("动画演示暂停");
        alert.setContentText("您暂停了当前动画,想要继续点击继续即可");
        alert.showAndWait();
    }

    public void callContinuePlayAlert(){
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setHeaderText("继续动画演示");
        alert.setContentText("您继续了动画演示");
        alert.showAndWait();
    }

    public void callMutiCallPlayAlert(){
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setContentText("在开始后请不要多次点击开始");
        alert.showAndWait();
    }



}